package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.servlet.http.Cookie;
<<<<<<< HEAD
import javax.servlet.http.HttpServletRequest;
=======
>>>>>>> a81cc8d... lijun
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/personalCenter")
public class MemberController {
    // 注入
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private MemberService memberService;

    @RequestMapping("/login")
    public Result login(HttpServletResponse response, @RequestBody Map map) {
        String telephone = (String) map.get("telephone");
        String validateCode = (String) map.get("validateCode");
        //从redis中获取验证码
        Jedis resource = jedisPool.getResource();
        String s = resource.get(telephone + RedisMessageConstant.SENDTYPE_LOGIN);
        resource.close();
        if (s != null && validateCode != null && s.equals(validateCode)) {
            //验证码输入正确
            //判断当前是不是会员
            Member member = memberService.findByTelephone(telephone);
            if (member == null) {
                member = new Member();
                //不是会员
                member.setRegTime(new Date());
                member.setPhoneNumber(telephone);
                memberService.add(member);
            }
            //向客户端浏览器写入Cookie,内容为手机号,目的跟踪用户
            Cookie cookie = new Cookie("login_member_telephone", telephone);
            //设置路径,这个是代表根路径下面的,都会有cookie,假如cookie.setPath("/pages");那表示只有pages下面的才有cookie
            cookie.setPath("/");
            //设置存活时间
            cookie.setMaxAge(60 * 60 * 24 * 30);
            //将cookie写入到浏览器中
            response.addCookie(cookie);
            //将会员信息保存到redis中
            String json = JSON.toJSON(member).toString();
            Jedis resource1 = jedisPool.getResource();
            resource1.setex(telephone, 60 * 30, json);
            resource1.close();
            return new Result(true, MessageConstant.LOGIN_SUCCESS);
        } else {
            //验证码输入错误
            return new Result(false, MessageConstant.VALIDATECODE_ERROR);
        }
    }


    /**
     * @Description: 从Redis中获取会员(用户)信息
     * @Author: LiuTaoTao
     * @Date: 2020 11
     * @Param: HttpServletRequest
     * @Return: Result
     */
    @RequestMapping("/findMemberByRedis")
    public Result findMemberByRedis(HttpServletRequest httpServletRequest) {
        // 获取一个cookies对象
        Cookie[] cookies = httpServletRequest.getCookies();
        // 提升作用域
        String telephone = null;
        // 判断cookie是否存在
        if (cookies != null) {
            // 有cookie则遍历cookie
            for (Cookie cookie : cookies) {
                // 如果遍历出来的name名称==登录时输入的name
                if ("login_member_telephone".equals(cookie.getName())) {
                    // 定义变量接收将用户输入的手机号
                    telephone = cookie.getValue();
                }
            }
        }
        // 创建一个连接池
        Jedis resource1 = jedisPool.getResource();
        // 从连接池中获取手机号,并返回jeson
        String json = resource1.get(telephone);
        resource1.close();
        // 返回给前端
        return new Result(true, "用户信息展示成功", JSONObject.parseObject(json));
    }


    /**
     * @Description: 移动端更新会员信息
     * @Author: LiuTaoTao
     * @Date: 2020 11
     * @Param:
     * @Return:
     */
    @RequestMapping("/updatePersonalData")
    public Result updatePersonalData(@RequestBody Member member) {
        try {
            memberService.updatePersonalData(member);
            return new Result(true, MessageConstant.EDIT_MEMBER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_MEMBER_FAIL);
        }
    }
}